/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmDaftarPasienRumahSakit.java
 *
 * Created on Jan 24, 2012, 8:27:17 PM
 */
package SIMRS.Apotek.view;

import SIMRS.Apotek.sql.KoneksiDatabase;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;
import usu.widget.Form;

/**
 *
 * @author yunniie
 */
public class frmDaftarPasienRumahSakit extends Form {
private static DefaultTableModel model;
    /** Creates new form frmDaftarPasienRumahSakit */
    public frmDaftarPasienRumahSakit() {
        initComponents();
        
         model=new DefaultTableModel();
        tblDaftarPasien.setModel(model);
        model.addColumn("No Resep");
        model.addColumn("Tanggal");
        model.addColumn("No Registrasi");
        model.addColumn("No RM");
        model.addColumn("Nama Pasien");
        model.addColumn("Dokter");
        model.addColumn("Ruangan Asal");
        model.addColumn("User");
        model.addColumn("Nama Obat");
        model.addColumn("Jumlah");
        model.addColumn("Harga");
        
        
        Date tanggal=new Date();
         tanggal.getDate();
        txtTanggalAwal.setDate(tanggal);
        txtTanggalAkhir.setDate(tanggal);
        
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/icon.png")).getImage());
        setTitle(".:: Form Pelayanan Resep Pasien");
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlForm1 = new SIMRS.Apotek.widget.PnlForm();
        label1 = new SIMRS.Apotek.widget.Label();
        label2 = new SIMRS.Apotek.widget.Label();
        label3 = new SIMRS.Apotek.widget.Label();
        btnCari = new SIMRS.Apotek.widget.button2();
        scrollPane1 = new SIMRS.Apotek.widget.ScrollPane();
        tblDaftarPasien = new SIMRS.Apotek.widget.Table();
        btnTutup = new SIMRS.Apotek.widget.button2();
        btnPelayananResep = new SIMRS.Apotek.widget.button2();
        txtTanggalAkhir = new com.toedter.calendar.JDateChooser();
        txtTanggalAwal = new com.toedter.calendar.JDateChooser();
        btnCetak = new SIMRS.Apotek.widget.button2();

        setTitle(".:: Form Data Pasien");
        setAnimationHide(usu.widget.constan.Animation.HIDE_TO_LEFT);
        setAnimationShow(usu.widget.constan.Animation.SHOW_FROM_LEFT);
        setResizable(false);
        setUndecorated(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnlForm1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(57, 190, 53), 3, true));
        pnlForm1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        label1.setForeground(new java.awt.Color(255, 255, 255));
        label1.setText("Daftar Pasien Rumah Sakit");
        label1.setFont(new java.awt.Font("Tahoma", 1, 18));
        pnlForm1.add(label1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, 260, -1));

        label2.setText("Periode");
        pnlForm1.add(label2, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 100, 92, -1));

        label3.setText("S/d");
        pnlForm1.add(label3, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 120, -1, 21));

        btnCari.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_search.png"))); // NOI18N
        btnCari.setText("Cari");
        btnCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariActionPerformed(evt);
            }
        });
        pnlForm1.add(btnCari, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 110, 100, -1));

        tblDaftarPasien.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scrollPane1.setViewportView(tblDaftarPasien);

        pnlForm1.add(scrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 660, 160));

        btnTutup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_close.png"))); // NOI18N
        btnTutup.setText("Tutup");
        btnTutup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTutupActionPerformed(evt);
            }
        });
        pnlForm1.add(btnTutup, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 330, -1, -1));

        btnPelayananResep.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/Automator Launcher.png"))); // NOI18N
        btnPelayananResep.setText("Pelayanan Resep");
        btnPelayananResep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPelayananResepActionPerformed(evt);
            }
        });
        pnlForm1.add(btnPelayananResep, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 330, -1, 40));
        pnlForm1.add(txtTanggalAkhir, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 120, 120, -1));
        pnlForm1.add(txtTanggalAwal, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 120, 120, -1));

        btnCetak.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SIMRS/Apotek/gambar/bt_cetak.png"))); // NOI18N
        btnCetak.setText("Cetak");
        btnCetak.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCetakActionPerformed(evt);
            }
        });
        pnlForm1.add(btnCetak, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 330, 110, 40));

        getContentPane().add(pnlForm1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 680, 390));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-680)/2, (screenSize.height-385)/2, 680, 385);
    }// </editor-fold>//GEN-END:initComponents

    private void btnTutupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTutupActionPerformed
        dispose();
    }//GEN-LAST:event_btnTutupActionPerformed

private void btnPelayananResepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPelayananResepActionPerformed
// TODO add your handling code here:
    input();
}//GEN-LAST:event_btnPelayananResepActionPerformed

private void btnCariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCariActionPerformed
// TODO add your handling code here:
    SimpleDateFormat Format=new SimpleDateFormat("yyyy-MM-dd");
    String tanggal1=Format.format(txtTanggalAwal.getDate());
    String tanggal2=Format.format(txtTanggalAkhir.getDate());
    
  model.getDataVector().removeAllElements();
    model.fireTableDataChanged();
    Connection c = KoneksiDatabase.getKoneksi();
    try {
        Statement s = c.createStatement();
        String sql = "SELECT transaksiresep.NoResep, resepobat.TglResep, transaksiresep.NoRegistrasi, transaksiresep.NoRM, pasien.NamaPasien, resepobat.IdDokter, resepobat.KdRuanganAsal, pegawai.Nama, obat.NamaBarang, transaksiresep.JmlBarang, transaksiresep.Harga from resepobat, pasien, transaksiresep, obat, pegawai where (transaksiresep.NoRM=pasien.NoRM) AND (pegawai.IdPegawai=resepobat.IdUser) AND (resepobat.NoResep=transaksiresep.NoResep) AND (obat.KdBarang=transaksiresep.KdBarang) AND (TglResep between '" + tanggal1 + "'and '" + tanggal2 + "') ";
        ResultSet rs = s.executeQuery(sql);
        while (rs.next()) {

            Object[] o = new Object[11];

            o[0] = rs.getString("NoResep");
            o[1] = rs.getString("TglResep");
            o[2] = rs.getString("NoRegistrasi");
            o[3] = rs.getString("NoRM");
            o[4] = rs.getString("NamaPasien");
            o[5] = rs.getString("IdDokter");
            o[6] = rs.getString("KdRuanganAsal");
            o[7] = rs.getString("Nama");
            o[8] = rs.getString("NamaBarang");
            o[9] = rs.getString("JmlBarang");
            o[10] = rs.getString("Harga");


            model.addRow(o);
        }
        
        rs.close();
        
        
    } catch (SQLException ex) {
        System.out.println("Error: "+ ex);
        
    }
}//GEN-LAST:event_btnCariActionPerformed

private void btnCetakActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCetakActionPerformed
// TODO add your handling code here:
    try {
            // TODO add your handling code here:
                String ReportName = "SIMRS/Apotek/report/LaporanPelayananObatPasien.jasper";
                InputStream is = this.getClass().getClassLoader().getResourceAsStream(ReportName);
                
                JasperPrint jasperPrint = JasperFillManager.fillReport(is, null, KoneksiDatabase.getKoneksi());
                JRViewer jv = new JRViewer(jasperPrint);
                
                JFrame jf = new JFrame();
                jf.getContentPane().add(jv);
                jf.validate();
                java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
                jf.setSize(screenSize);
                jf.setLocation(0, 0);
                jf.setTitle("Cetak Data Obat Pasien");
                
                jf.setVisible(true);
        } catch (JRException ex) {
            Logger.getLogger(frmDaftarPasienRumahSakit.class.getName()).log(Level.SEVERE, null, ex);
            
        }
}//GEN-LAST:event_btnCetakActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private SIMRS.Apotek.widget.button2 btnCari;
    private SIMRS.Apotek.widget.button2 btnCetak;
    private SIMRS.Apotek.widget.button2 btnPelayananResep;
    private SIMRS.Apotek.widget.button2 btnTutup;
    private SIMRS.Apotek.widget.Label label1;
    private SIMRS.Apotek.widget.Label label2;
    private SIMRS.Apotek.widget.Label label3;
    private SIMRS.Apotek.widget.PnlForm pnlForm1;
    private SIMRS.Apotek.widget.ScrollPane scrollPane1;
    private SIMRS.Apotek.widget.Table tblDaftarPasien;
    private com.toedter.calendar.JDateChooser txtTanggalAkhir;
    private com.toedter.calendar.JDateChooser txtTanggalAwal;
    // End of variables declaration//GEN-END:variables

public void input() {
    int i = tblDaftarPasien.getSelectedRow();
        if(i==-1){
            return;
        }
       
            String noResep=tblDaftarPasien.getValueAt(i, 0).toString();
            String noReg=tblDaftarPasien.getValueAt(i, 2).toString();
            String noRM=tblDaftarPasien.getValueAt(i, 3).toString();
            String pasien=tblDaftarPasien.getValueAt(i, 4).toString();
       
        Connection c=KoneksiDatabase.getKoneksi();
         
         String dokter=tblDaftarPasien.getValueAt(i, 5).toString();
         
         String ruang=tblDaftarPasien.getValueAt(i, 6).toString();
        try {
            Statement stm=c.createStatement();
            String sql="SELECT Nama FROM pegawai where IdPegawai = '" + dokter  + "'";
            ResultSet rsRuangan=stm.executeQuery(sql);

            rsRuangan.beforeFirst();
            while (rsRuangan.next()) {                
                 dokter= rsRuangan.getString("Nama");
            }
            rsRuangan.close();
        } catch (Exception e) {
            
        } 
        try {
            Statement stm=c.createStatement();
            String sql2="SELECT NamaRuangan FROM ruangan where KdRuangan = '" + ruang  + "'";
            ResultSet rsRuanganT=stm.executeQuery(sql2);

            rsRuanganT.beforeFirst();
            while (rsRuanganT.next()) {                
                 ruang= rsRuanganT.getString("NamaRuangan");
                
            }
            rsRuanganT.close();
        } catch (Exception e) {
            
        } 

        SimpleDateFormat tgl = new SimpleDateFormat("yyyy-MM-dd");
                Date a = null;

                String tanggal = tblDaftarPasien.getValueAt(i, 1).toString();
                try {
                    a = tgl.parse(tanggal);
                } catch (ParseException e) {
                   
                }
    
      String nmBarang=tblDaftarPasien.getValueAt(i, 8).toString();
       String jmlh=tblDaftarPasien.getValueAt(i, 9).toString();
       String harga=tblDaftarPasien.getValueAt(i, 10).toString();
       String user=tblDaftarPasien.getValueAt(i, 7).toString();
        try {
            Statement stm=c.createStatement();
            String sql3="SELECT Nama FROM pegawai where IdPegawai = '" + user + "'";
            ResultSet rsPenerima=stm.executeQuery(sql3);

            rsPenerima.beforeFirst();
            while (rsPenerima.next()) {                
                 user= rsPenerima.getString("Nama");
               
            }
            rsPenerima.close();
        } catch (Exception e) {
            
        } 

       
     //  String tot=tblInformasiPengirimanBarangRuangan.getValueAt(i, 6).toString();
       
    frmPelayananResepPasien form= new frmPelayananResepPasien();
    form.isiText(noResep, noReg, noRM, pasien, dokter, user, ruang, user, nmBarang, jmlh, harga, a, noRM);
    form.setVisible(true);
}

    
}
